#ifndef     __RT_TASK2_H
#define     __RT_TASK2_H

#include "./promise_type2.h"
#include "./task_awaiter2.h"
#include <utility>

namespace rt {

template <typename T = void>
class Task2 {

public:
    using THIS = Task2<T>;  
    using promise_type = PromiseType2<THIS, T>;
    Task2() = default;
    Task2(std::coroutine_handle<promise_type> handle) : m_handle(handle) {}
    Task2(const THIS&) = delete;
    Task2(THIS&& other) : m_handle(other.m_handle) {
        other.m_handle = nullptr;
    }
    THIS& operator=(THIS&& other) {
        std::swap(m_handle, other.m_handle);
        return *this;
    }

    TaskAwaiter2<promise_type, T> operator co_await() {
        return { m_handle };
    }

    ~Task2() {
        if (m_handle) {
            m_handle.destroy();
        }
    }

private:
    std::coroutine_handle<promise_type> m_handle;
};

}

#endif
